package com.epam.kulibaba.helpers.testSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;

import com.epam.kulibaba.components.catalog.ProductsCatalog;
import com.epam.kulibaba.components.catalog.linkSpecialOffers.LinkToSpecialOffers;
import com.epam.kulibaba.components.compare.panelLevelUp.PanelLevelUp;
import com.epam.kulibaba.components.compareTable.CompareTable;
import com.epam.kulibaba.components.header.HeaderPage;
import com.epam.kulibaba.components.productDescription.ProductDescription;
import com.epam.kulibaba.components.specialPrice.SpecialPrice;

public class ProductDescriptionHelper {

	public static void goToCompare(ProductsCatalog productsCatalog) {
		Reporter.log("Go to Compare");
		productsCatalog.getCompareLink().click();
	}

	public static HashMap<String, String> getProductOptions(
			ProductsCatalog productCatalog, int productNumber,
			ProductDescription itemDescription, PanelLevelUp panelLevelUp) {
		Reporter.log("get product " + productNumber + " options");
		HashMap<String, String> itemOptions = null;
		ProductsListHelper.goToProductPage(productCatalog, productNumber);
		itemOptions = ProductDescriptionHelper.getDescription(itemDescription);
		PanelLevelUp.goToCategoryLevelUp(panelLevelUp);
		return itemOptions;
	}

	public static HashMap<String, String> getDescription(
			ProductDescription productDescription) {
		Reporter.log("Get product description on product page");
		HashMap<String, String> productDescriptions = new HashMap<String, String>();
		List<WebElement> rowDescriptions = productDescription
				.getItemDescription().findElements(By.cssSelector("div.row"));
		for (WebElement row : rowDescriptions) {
			String name = row.findElement(By.cssSelector("span.pr")).getText()
					.toLowerCase();
			String value = row.findElement(By.cssSelector("span.val"))
					.getText().toLowerCase();
			productDescriptions.put(name, value);
		}
		/*
		 * for (Map.Entry me : productDescriptions.entrySet()) {
		 * System.out.println(me.getKey() + " " + me.getValue()); }
		 */
		return productDescriptions;
	}

	public static void checkDescriptionEqualsCompareDescription(
			HashMap<String, String> productDescriptions,
			HashMap<String, String> productDescriptionsCompare) {
		Reporter.log("check product description on product page Equals product description on compare page");
		for (Entry<String, String> entry : productDescriptions.entrySet()) {
			String key = entry.getKey();
			Assert.assertEquals(
					entry.getValue()
							.equals(productDescriptionsCompare.get(key)), true);
		}
	}

	public static HashMap<String, String> productDescriptionsInCompare(
			CompareTable compareTable, int productNumber) {
		Reporter.log("Get product description on compare page");
		HashMap<String, String> productCompareDescription = new HashMap<String, String>();
				List<WebElement> rowCompareDescriptions = compareTable
				.getCompareTable()
				.findElements(
						By.xpath("//tr[@class = ''] | //tr[@class = 'different']"));
		for (WebElement row : rowCompareDescriptions) {
			String name = row.findElement(By.xpath(".//td[1]")).getText()
					.toLowerCase();
			String value = row.findElement(By.xpath(".//td[last()]")).getText()
					.toLowerCase();
			productCompareDescription.put(name, value);
		}
		
		  /*System.out.println("-------------");
		  
		  for (Map.Entry me : productCompareDescription.entrySet()) {
		  System.out.println(me.getKey() + " " + me.getValue()); }*/
		 
		return productCompareDescription;
	}

	public static void checkColorDifferentDescriptions(
			CompareTable compareTable, Double numbersOfProducts,
			ProductsCatalog productCatalog) {
		Reporter.log("check different options in products on compare page");
		ProductsListHelper.goToProductsCompare(productCatalog,
				(int) numbersOfProducts.doubleValue());
		List<WebElement> rowCompareDescriptions = compareTable
				.getCompareTable()
				.findElements(
						By.xpath("//tr[@class = ''] | //tr[@class = 'different']"));
		for (WebElement row : rowCompareDescriptions) {
			List<WebElement> cells = row.findElements(By.xpath(".//td"));
			for (int cellIndex = 1; cellIndex < cells.size() - 1; cellIndex++) {
				if (!cells.get(cellIndex).getText()
						.equalsIgnoreCase(cells.get(cellIndex + 1).getText())) {
					Assert.assertEquals(row.getAttribute("class"), "different");
					/*
					 * System.out.println(cells.get(cellIndex).getText() +
					 * " >>>> " + cells.get(cellIndex + 1).getText());
					 */
					break;
				}
			}
		}
	}

	/*
	 * public static void checkLinksInSpecialOffers( ProductsCatalog
	 * productCatalog, int productNumber) {
	 * 
	 * ProductsListHelper.goToProductPage(productCatalog, productNumber); }
	 */

	public static void CheckAllProductsDescriptionsInCompare(
			Double numbersOfProducts, ProductsCatalog productCatalog,
			ProductDescription itemDescription,
			PanelLevelUp categoryMainPageLink, CompareTable compareTable,
			PanelLevelUp panelLevelUp) {
		for (int itemNumber = 1; itemNumber <= numbersOfProducts; itemNumber++) {
			Reporter.log("Get and check description  for product number "
					+ itemNumber + " in product catalog");
			HashMap<String, String> itemOptions = ProductDescriptionHelper
					.getProductOptions(productCatalog, itemNumber,
							itemDescription, categoryMainPageLink);
			ProductsListHelper.addProductToCompare(productCatalog, itemNumber);
			ProductsListHelper.goToProductsCompare(productCatalog, itemNumber);

			ProductDescriptionHelper.checkDescriptionEqualsCompareDescription(
					itemOptions, ProductDescriptionHelper
							.productDescriptionsInCompare(compareTable,
									itemNumber));
			PanelLevelUp.goToCategoryLevelUp(panelLevelUp);
		}

	}

	public static void CheckProductsInformation(WebDriver driver, ProductsCatalog productCatalog,
			ProductDescription itemDescription,
			PanelLevelUp categoryMainPageLink, Double numbersOfProducts, LinkToSpecialOffers linkBarPrice, HeaderPage header, HeaderPage searchButton, SpecialPrice resultTable) {
		for (int itemNumber = 1; itemNumber <= numbersOfProducts; itemNumber++) {			
			System.out.println("  dfgskl  " + itemNumber);
			 String productHref = ProductsListHelper.getLinkOnProductPage(productCatalog, itemNumber);
			 String productNameToSearch = ProductsListHelper.getNameToSearchProduct(productCatalog, itemNumber);
			
			 HashMap<String, String> itemOptions = ProductDescriptionHelper
					.getProductOptions(productCatalog, itemNumber,
							itemDescription, categoryMainPageLink);
			LinkToSpecialOffers.goToSpecialOffers(linkBarPrice);
			SearchPanelHelpers.searchProductByName(header, searchButton, productNameToSearch);
			SpecialOffersListHelper.checkDescriptionLinkInResultTable(productHref, resultTable);
			driver.navigate().back();
			driver.navigate().back();
		}
	}

	
}
